<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-3-21
 * Time: 下午4:00
 */
//首页控制器
namespace Service\Controller;
use Mcit\Page;

class IndexController extends BaseController {

    public function index(){

        $re        = strsToArray(session('location'));
        $location  = $re[0].",".$re[1];
        $char_o =M("char")->where(1)->select();
        $area = session("area");//当前运营商区号
        $strs = '';
        foreach($char_o as $w){
            $stss = substr($w['key_link'],0,4);
            if(substr($stss,0,2) == 00 && substr($w['key_link'],1,3) == $area){
                $strs .= $w['key_link'].",";
            }elseif($stss == $area){
                $strs .= $w['key_link'] .",";
            }
        }
        //当前运商下所有电桩
        $map['key_link'] = array('IN',rtrim($strs,','));

        $this->charAll   = D("Admin/State")->where($map)->count();//当前运营商下的电桩状态总数
        $map['charge']   = 1;
        $map['cable']    = 0;
        $map['error']    = 0;
        $map['bespeak']  = 0;
        $this->charge    = D("Admin/State")->where($map)->count();//充电中
        $map['charge']   = 0;
        $map['cable']    = 0;
        $map['error']    = 0;
        $map['bespeak']  = 0;
        $this->release   = D("Admin/State")->where($map)->count();//空闲
        $map['charge']   = 0;
        $map['cable']    = 0;
        $map['error']    = 1;
        $map['bespeak']  = 0;
        $this->error     = D("Admin/State")->where($map)->count();//故障下线

        //当前运营商下站点统计 运营中 在建中 规划中
        $cond['location']      = array('like','%'.$location.'%');
        $this->plant_online    = M("plant")->where($cond)->count();
        $cond['current_state'] = 2;
        $this->plant_building  = M("plant")->where($cond)->count();
        $cond['current_state'] = 3;
        $this->plant_coming    = M("plant")->where($cond)->count();

        $this->display();
    }
    //充电总数以及预约总数
    public function chart(){

        $re        = strsToArray(session('location'));
        $location  = array('like',$re[0].",".$re[1]."%");
        if(IS_AJAX){
            $condi['location'] = $location;
            if($_POST['type'] == "charge"){
                //最近15天充电总次数
                $charge = M("bill_total")->field("SUM(total) as total,currentTime")->where($condi)->group("currentTime")->order("currentTime desc")->limit(15)->select();
                echo json_encode($charge);
            }elseif($_POST['type'] == "bespeak"){
                //最近15天总预约数
                $bespeak = M("bespeak_total")->field("SUM(total) as total,currentTime")->where($condi)->group("currentTime")->order("currentTime desc")->limit(15)->select();
                echo json_encode($bespeak);
            }
        }
    }
}